package com.bs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.entity.AskLeave;
import com.bs.entity.AskLeaveHistory;
import com.bs.service.AskLeaveHistoryService;
import com.bs.service.AskLeaveService;
import com.bs.service.ClockInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/AskLeave")
public class AskLeaveCon {

    @Autowired
    AskLeaveService askLeaveService;

    @Autowired
    AskLeaveHistoryService askLeaveHistoryService;

    @GetMapping("/selectById")
    public AskLeave selectById(Integer id){
        return selectById(id);
    }

    @GetMapping("/selectALL")
    public List<AskLeave> selectAll(){
        return null;
    }

    @GetMapping("/agree")
    public boolean agree(@RequestParam Integer id){

        AskLeave entity = askLeaveService.getById(id);
        //删除审批表的数据
        askLeaveService.removeById(entity);
        entity.setPost(1);
        AskLeaveHistory askLeaveHistory = new AskLeaveHistory();
        BeanUtils.copyProperties(entity,askLeaveHistory);
        return askLeaveHistoryService.save(askLeaveHistory);
    }

    @GetMapping("/disAgree")
    public boolean disagree(@RequestParam Integer id){
        AskLeave entity = askLeaveService.getById(id);
        //删除审批表的数据
        askLeaveService.removeById(entity);
        entity.setPost(2);
        AskLeaveHistory askLeaveHistory = new AskLeaveHistory();
        BeanUtils.copyProperties(entity,askLeaveHistory);
        return askLeaveHistoryService.save(askLeaveHistory);
    }


    // 新增请假信息
    @PostMapping("/saveAskLeave")
    public Boolean saveAskLeaveInfo(AskLeave askLeave){
        return askLeaveService.saveAskLeaveInfo(askLeave);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean deleteAskLeaveInfo(@PathVariable Integer id){
        return askLeaveService.removeById(id);
    }


    //查询待审批的打卡信息
    @GetMapping("/page")
    public IPage<AskLeave> findPage(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , @RequestParam(required = false) Integer id){
        IPage<AskLeave> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AskLeave> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AskLeave::getPost,"0");

        if(Objects.nonNull(id)){
            queryWrapper.lambda().eq(AskLeave::getId,id);
        }
        return askLeaveService.page(page, queryWrapper);
    }

    // 查询打卡的历史信息表
    @GetMapping("/historyPage")
    public IPage<AskLeaveHistory> findHistoryPage(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , @RequestParam(required = false) Integer id){
        IPage<AskLeaveHistory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AskLeaveHistory> queryWrapper = new QueryWrapper<>();

        if(Objects.nonNull(id)){
            queryWrapper.lambda().eq(AskLeaveHistory::getId,id);

        }
        return askLeaveHistoryService.page(page, queryWrapper);
    }


}
